<?php
/**
 * Date: 2024.8
 * Author: kahn
 * 用户
 */
use config\Config;
use config\Response;
use JetBrains\PhpStorm\NoReturn;
use service\uc\CaptchaService;
use service\uc\SmsService;
use service\uc\UserService;

use models\uc\UserModel;

class UserController extends Controller
{
    /**
     * 手机号验证码注册/登录
     */
    #[NoReturn] public function loginAction(): void
    {
        $mobile = $this->_getParam('mobile', 'string');
        $code = $this->_getParam('code', 'string');
        $captcha = $this->_getParam('captcha', 'string', false);
        $uniqueId = $this->_getParam('uniqueId', 'string', false);

        if(!CaptchaService::verify($mobile, $captcha, $uniqueId)){
            $this->_response(Response::CAPTCHA_ERROR);
        }

        if(!SmsService::verify($mobile, $code)){
            //验证码错误, 设置图片验证码后返回
            $this->_returnOnSmsCodeError($mobile);
        }

        $ret = UserService::login($mobile, Config::REGISTER_LOGIN_TYPE_SMS, $userId, $token);
        if($ret != Response::SUCCESS){
            $this->_response($ret);
        }

        $this->_response($ret,  array('token'=>$token));
    }

    /**
     * token验证登录
     */
    #[NoReturn] public function verifyAction(): void
    {
        $user = UserModel::find($this->userId);
        $retCode = Response::SYSTEM_ERROR;
        if($user->isEmpty() || !UserService::check($user, $retCode)){
            $this->_response($retCode);
        }

        if(!UserService::loginLog($this->userId, Config::REGISTER_LOGIN_TYPE_TOKEN_VERIFY)){
            $this->_response(Response::SYSTEM_ERROR);
        }

        $this->_response(Response::SUCCESS);
    }

    /**
     * 退出登录
     */
    #[NoReturn] public function logoutAction(): void
    {
        if(!UserService::logout($this->userId)){
            $this->_response(Response::SYSTEM_ERROR);
        }
        $this->_response(Response::SUCCESS);
    }

    /**
     * 账号注销
     */
    #[NoReturn] public function logoffAction(): void
    {
        $code = $this->_getParam('code', 'string');
        $captcha = $this->_getParam('captcha', 'string', false);
        $uniqueId = $this->_getParam('uniqueId', 'string', false);

        $user = UserModel::find($this->userId);
        $retCode = Response::SYSTEM_ERROR;
        if($user->isEmpty() || !UserService::check($user, $retCode)){
            $this->_response($retCode);
        }
        $mobile = $user->mobile;

        //如果需要图片验证码验证, 则进行验证
        if(!CaptchaService::verify($mobile, $captcha, $uniqueId)){
            //验证码错误, 设置图片验证码后返回
            $this->_response(Response::CAPTCHA_ERROR);
        }

        if(!SmsService::verify($mobile, $code)){
            $this->_returnOnSmsCodeError($mobile);
        }

        $retCode = UserService::logoff($user);
        $this->_response($retCode);
    }

    private function _returnOnSmsCodeError($mobile): void
    {
        if(!CaptchaService::isOpen()){
            $this->_response(Response::SMS_CODE_ERROR);
        }else{
            //生成图片验证码
            if(!CaptchaService::generate($uniqueId, $img64)){
                $this->_response(Response::SYSTEM_ERROR);
            }

            //手机验证码错误, 下次输入验证码前需要进行图片验证码校验
            CaptchaService::setInVerify($mobile);

            $this->_response(Response::SMS_CODE_ERROR, ['uniqueId'=>$uniqueId, 'img64'=>$img64]);
        }
    }

}

